package net.stevechaloner.idea.ffs;

import org.jetbrains.annotations.PropertyKey;
import org.jetbrains.annotations.NonNls;

import java.util.ResourceBundle;
import java.lang.ref.SoftReference;
import java.lang.ref.Reference;

import com.intellij.CommonBundle;

/**
 * I18N support.
 */
public class FirefoxSearchBundle
{
    /**
     * The bundle.
     */
    private static Reference<ResourceBundle> bundle;

    /**
     * The basename of the bundle.
     */
    @NonNls
    private static final String BUNDLE = "net.stevechaloner.idea.ffs.ffs-i18n";

    /**
     * Factory class
     */
    private FirefoxSearchBundle()
    {
    }

    /**
     * Gets the message for the given key with any parameters
     * substituted in place.
     *
     * @param key    the key of the message
     * @param params parameters for the message
     * @return the formatted message
     */
    public static String message(@PropertyKey(resourceBundle =
            BUNDLE)String key,
                                 Object... params)
    {
        return CommonBundle.message(getBundle(),
                                    key,
                                    params);
    }

    /**
     * Loads the bundle.
     *
     * @return the bundle
     */
    private static ResourceBundle getBundle()
    {
        ResourceBundle bundle = null;
        if (FirefoxSearchBundle.bundle != null)
        {
            bundle = FirefoxSearchBundle.bundle.get();
        }
        if (bundle == null)
        {
            bundle = ResourceBundle.getBundle(BUNDLE);
            FirefoxSearchBundle.bundle = new SoftReference<ResourceBundle>(bundle);
        }
        return bundle;
    }
}
